package com.zhijiang.hospital.service.impl;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zhijiang.hospital.entity.ArrangerClass;
import com.zhijiang.hospital.entity.dto.EmployeeArrangerClassDto;
import com.zhijiang.hospital.mapper.ArrangerClassMapper;
import com.zhijiang.hospital.mapper.CheckinMapper;
import com.zhijiang.hospital.mapper.EmployeeArrangerClassDtoMapper;
import com.zhijiang.hospital.service.ArrangerClassService;
import com.zhijiang.util.ArrangerClassQuery;
import com.zhijiang.util.NumberUtils;
import com.zhijiang.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 75936
 * @version 1.0
 * @project zhijiang_hospital_datas
 * @description
 * @date 2023-04-11 10:18:15
 */
@Service
public class ArrangerClassServiceImpl implements ArrangerClassService {
    @Autowired
    private ArrangerClassMapper arrangerClassMapper;
    @Autowired
    private EmployeeArrangerClassDtoMapper employeeArrangerClassDtoMapper;

    @Override
    public PageList<ArrangerClass> getArrangerClass(ArrangerClassQuery arrangerClassQuery) {
        List<ArrangerClass> list = arrangerClassMapper.getArrangerClass(arrangerClassQuery.getSquenceTime(), arrangerClassQuery.getEmpName(), arrangerClassQuery.getDeptName());
        List<EmployeeArrangerClassDto> employeeArrangerClassDtos = employeeArrangerClassDtos(arrangerClassQuery);

        //Map<String, Object> map = Maps.newHashMap();
        List<ArrangerClass> collect = list.stream().skip((arrangerClassQuery.getPage() - 1) * arrangerClassQuery.getPageSize()).limit(arrangerClassQuery.getPageSize()).collect(Collectors.toList());
        for (ArrangerClass arrangerClass : collect) {
            for (EmployeeArrangerClassDto employeeArrangerClassDto : employeeArrangerClassDtos) {
                if (arrangerClass.getEmpId().equals(employeeArrangerClassDto.getEmplCode())) {
                    arrangerClass.setEmplType(employeeArrangerClassDto.getEmplType());
                }
            }
        }
        PageList<ArrangerClass> pageList = new PageList<>();
        pageList.setList(collect);
        pageList.setPage(arrangerClassQuery.getPage());
        pageList.setTotal(Long.valueOf(String.valueOf(list.size())));
        pageList.setPage(arrangerClassQuery.getPage());
        pageList.setPageSize(arrangerClassQuery.getPageSize());
        pageList.setPages(NumberUtils.totalPages(Long.valueOf(String.valueOf(list.size())), arrangerClassQuery.getPageSize()));
        return pageList;
    }


    @Resource
    CheckinMapper checkinMapper;

    @Override
    public PageList<ArrangerClass> contrast(ArrangerClassQuery arrangerClassQuery) throws ParseException {

        List<ArrangerClass> list = arrangerClassMapper.getArrangerClass(arrangerClassQuery.getSquenceTime(), arrangerClassQuery.getEmpName(), arrangerClassQuery.getDeptName()).stream().sorted(Comparator.comparing(ArrangerClass::getSquenceTime).reversed()).collect(Collectors.toList());
        //List<ArrangerClass> collect1 = list.stream().sorted(Comparator.comparing(ArrangerClass::getSquenceTime).reversed()).collect(Collectors.toList());
        List<EmployeeArrangerClassDto> employeeArrangerClassDtos = employeeArrangerClassDtos(arrangerClassQuery);

        String squenceTime = arrangerClassQuery.getSquenceTime();
//        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Date parse = ft.parse(squenceTime);
        //Map<String, Object> map = Maps.newHashMap();
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");

        List<ArrangerClass> collect = list.stream().collect(Collectors.toList());
        //List<ArrangerClass> collect = list.stream().skip((arrangerClassQuery.getPage() - 1) * arrangerClassQuery.getPageSize()).limit(arrangerClassQuery.getPageSize()).collect(Collectors.toList());
        collect.stream().forEach(s -> {
            try {
                Date date1 = new Date(ft.parse(s.getSquenceTime()).getTime());
                date1.setHours(0);
                date1.setMinutes(0);
                date1.setSeconds(0);
                Date date2 = new Date(ft.parse(s.getSquenceTime()).getTime());
                date2.setHours(23);
                date2.setMinutes(59);
                date2.setSeconds(59);

                List<ArrangerClass> arrangerClass = checkinMapper.getCheckOne(s.getEmpName(), date1, date2);
                if (arrangerClass.size() >= 2) {
                    s.setCheckType("正常");
                } else if (arrangerClass.size() == 1) {
                    s.setCheckType("异常");
                } else if (arrangerClass.size() == 0) {
                    s.setCheckType("未打卡");
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }

//            if (arrangerClass.size()>=2){
//                s.setCheckType("正常");
//            }else if (arrangerClass.size()==1){
//                s.setCheckType("异常");
//            }else if (arrangerClass.size()==0){
//                s.setCheckType("未打卡");
//            }
            // s.setCheckType(arrangerClass.getCheckType());
        });
        for (ArrangerClass arrangerClass : collect) {

            for (EmployeeArrangerClassDto employeeArrangerClassDto : employeeArrangerClassDtos) {
                if (arrangerClass.getEmpId().equals(employeeArrangerClassDto.getEmplCode())) {
                    arrangerClass.setEmplType(employeeArrangerClassDto.getEmplType());
                }
            }
        }

        PageList<ArrangerClass> pageList = new PageList<>();
        pageList.setList(collect);
        pageList.setPage(arrangerClassQuery.getPage());
        pageList.setTotal(Long.valueOf(String.valueOf(list.size())));
        pageList.setPage(arrangerClassQuery.getPage());
        pageList.setPageSize(arrangerClassQuery.getPageSize());
        pageList.setPages(NumberUtils.totalPages(Long.valueOf(String.valueOf(list.size())), arrangerClassQuery.getPageSize()));
        return pageList;
    }

    @Override

    public Map<String, List<ArrangerClass>> contrast1(ArrangerClassQuery arrangerClassQuery) {
        List<ArrangerClass> list = new ArrayList<>();
        System.out.println("初始化" + arrangerClassQuery.getDeptName());
        if (arrangerClassQuery.getDeptName().equals("全部打卡科室")) {

            list = arrangerClassMapper.getArrangerClass3(arrangerClassQuery.getSquenceTime(), arrangerClassQuery.getEmpName());
        } else {
            System.out.println("初始化" + arrangerClassQuery.getDeptName());
            list = arrangerClassMapper.getArrangerClass(arrangerClassQuery.getSquenceTime(), arrangerClassQuery.getEmpName(), arrangerClassQuery.getDeptName()).stream().sorted(Comparator.comparing(ArrangerClass::getSquenceTime).reversed()).collect(Collectors.toList());

        }


        PageList<ArrangerClass> pageList = new PageList<>();
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");

        list.stream().forEach(s -> {
            try {
                Date date1 = new Date(ft.parse(s.getSquenceTime()).getTime());
                date1.setHours(0);
                date1.setMinutes(0);
                date1.setSeconds(0);
                Date date2 = new Date(ft.parse(s.getSquenceTime()).getTime());
                date2.setHours(23);
                date2.setMinutes(59);
                date2.setSeconds(59);
                Date mDate = new Date(ft.parse(s.getSquenceTime()).getTime());
                mDate.setHours(8);
                mDate.setMinutes(0);
                mDate.setSeconds(0);
                Date aDate = new Date(ft.parse(s.getSquenceTime()).getTime());
                aDate.setHours(17);
                aDate.setMinutes(30);
                aDate.setSeconds(0);
                List<ArrangerClass> arrangerClass = checkinMapper.getCheckOne(s.getEmpName(), date1, date2);

                System.out.println(arrangerClass);

                if (arrangerClass.size() >= 2) {

                    //计算   下班打卡时间    上班打卡时间
                    if (mDate.getTime() < arrangerClass.get(0).getCheckDate().getTime()) {
                        s.setMorningCheckType("迟到");
                    } else {
                        s.setMorningCheckType(arrangerClass.get(0).getCheckType());
                    }

                    if (aDate.getTime() > arrangerClass.get(1).getCheckDate().getTime()) {
                        s.setAfternoonCheckType("早退");
                    } else {
                        s.setAfternoonCheckType(arrangerClass.get(1).getCheckType());
                    }
                    // s.setMorningCheckType(arrangerClass.get(0).getCheckType());
//                    s.setAfternoonCheckType(arrangerClass.get(1).getCheckType());
//                    System.out.println(arrangerClass.get(0).getCheckType());

                    s.setCheckType("正常");
                } else if (arrangerClass.size() == 1) {
                    //计算   下班打卡时间    上班打卡时间
                    if (mDate.getTime() < arrangerClass.get(0).getCheckDate().getTime()) {
                        s.setMorningCheckType("迟到");
                    } else {
                        s.setMorningCheckType(arrangerClass.get(0).getCheckType());
                    }


                    s.setAfternoonCheckType("未打卡");
                    s.setCheckType("异常");
                } else if (arrangerClass.size() == 0) {

                    s.setCheckType("未打卡");
                    s.setMorningCheckType("未打卡");
                    s.setAfternoonCheckType("未打卡");
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }

        });
        List<EmployeeArrangerClassDto> employeeArrangerClassDtos = employeeArrangerClassDtos(arrangerClassQuery);
        for (ArrangerClass arrangerClass : list) {

            for (EmployeeArrangerClassDto employeeArrangerClassDto : employeeArrangerClassDtos) {
                if (arrangerClass.getEmpId().equals(employeeArrangerClassDto.getEmplCode())) {
                    arrangerClass.setEmplType(employeeArrangerClassDto.getEmplType());
                }
            }
        }
        Map<String, List<ArrangerClass>> collect = list.stream().collect(Collectors.groupingBy(ArrangerClass::getDeptName));

        return collect;

    }

    @Override
    public List<String> deptAll() {
        List<String> deptAll = arrangerClassMapper.deptAll();
        return deptAll;
    }

    public List<EmployeeArrangerClassDto> employeeArrangerClassDtos(ArrangerClassQuery arrangerClassQuery) {
        List<EmployeeArrangerClassDto> employeeArrangerClassDtos = employeeArrangerClassDtoMapper.getEmployeeArrangerClassDtos();
        return employeeArrangerClassDtos;
    }
}
